package yxy.game.pm2.active.module.subordinateclass.weeklycard;

import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.actweekcarkgrow;
import yxy.cherry.data.bean.actweekcarktask;
import yxy.cherry.data.bean.task;
import yxy.game.pm2.active.module.actdata.ActDataCenter;
import yxy.game.pm2.bean.task.Task;
import yxy.game.pm2.module.tip.TipHandle;
import yxy.game.pm2.module.tip.TipType;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * 周卡活动 - 活跃任务
 * @author Dell
 *
 */
public class WeeklyActiveTask {

	//bean ---------------------------------------------------------
	
	//共用
	private int keynum;			//钥匙数量   货币36转化
	
	//宝箱
	private ArrayList<Integer> growids = new ArrayList<Integer>();		//表格id
	private ArrayList<Integer> growgetids = new ArrayList<Integer>();	//领取ids
	
	//活跃任务
	private int taskgroup;
	private HashMap<Integer, ActiveTask> atmap = new HashMap<Integer, ActiveTask>();
	
	public int getKeynum() {
		return keynum;
	}
	public void setKeynum(int keynum) {
		this.keynum = keynum;
	}
	public ArrayList<Integer> getGrowids() {
		return growids;
	}
	public void setGrowids(ArrayList<Integer> growids) {
		this.growids = growids;
	}
	public ArrayList<Integer> getGrowgetids() {
		return growgetids;
	}
	public void setGrowgetids(ArrayList<Integer> growgetids) {
		this.growgetids = growgetids;
	}
	public int getTaskgroup() {
		return taskgroup;
	}
	public void setTaskgroup(int taskgroup) {
		this.taskgroup = taskgroup;
	}
	public HashMap<Integer, ActiveTask> getAtmap() {
		return atmap;
	}
	public void setAtmap(HashMap<Integer, ActiveTask> atmap) {
		this.atmap = atmap;
	}
	
	//func ------------------------------------------------------------
	
	public static WeeklyActiveTask create(String playercode) {
		WeeklyActiveTask wat = new WeeklyActiveTask();

		wat.setKeynum(0);
		
		//宝箱
		Map<String, actweekcarkgrow> awcgmap = ActDataCenter.getAllData(actweekcarkgrow.class, playercode);
		for(actweekcarkgrow data :awcgmap.values()) {
			wat.getGrowids().add(data.getId());
		}
		
		//活跃任务
		Map<String, actweekcarktask> awctmap = ActDataCenter.getAllData(actweekcarktask.class, playercode);
		ArrayList<Integer> groups = new ArrayList<Integer>();
 		for(actweekcarktask data : awctmap.values()) {
			if(groups.contains(data.getGroup())) {
				continue;
			}
			
			groups.add(data.getGroup());
		}
 		
 		int group = 1;
 		if(groups.size() > 0) {
 			Collections.sort(groups);
 			group = groups.get(0);
 		}
 		wat.setTaskgroup(group);
 		
 		for(actweekcarktask data : awctmap.values()) {
			if(group != data.getGroup()) {
				continue;
			}

			int taskid = data.getTaskid();
			task taskdata = DataCenter.getData(taskid, task.class);
			if(taskdata == null) {
				continue;
			}
			
			Task taskbean = Task.create(playercode, taskdata);
			
			ActiveTask at = new ActiveTask();
			at.setId(data.getId());
			at.setTaskid(data.getTaskid());
			at.setTaskcode(taskbean.getCode());
//			at.setHadget(false);
			wat.getAtmap().put(data.getId(), at);
 		}
		
		return wat;
	}
	
	
	public void refresh(String playercode) {
		for(ActiveTask at : getAtmap().values()) {
			Task.remove(playercode, at.getTaskcode());
		}
		
		setAtmap(new HashMap<Integer, ActiveTask>());
		
		// 活跃任务
		Map<String, actweekcarktask> awctmap = ActDataCenter.getAllData(actweekcarktask.class, playercode);
		ArrayList<Integer> groups = new ArrayList<Integer>();
		for (actweekcarktask data : awctmap.values()) {
			if (groups.contains(data.getGroup())) {
				continue;
			}

			groups.add(data.getGroup());
		}

		int newgroup = getTaskgroup() + 1;
		if (!groups.contains(newgroup)) {
			Collections.sort(groups);
			newgroup = groups.get(0);
		}
		setTaskgroup(newgroup);

		for (actweekcarktask data : awctmap.values()) {
			if (newgroup != data.getGroup()) {
				continue;
			}

			int taskid = data.getTaskid();
			task taskdata = DataCenter.getData(taskid, task.class);
			if (taskdata == null) {
				continue;
			}

			Task taskbean = Task.create(playercode, taskdata);
			if(taskbean == null) {
				System.out.println(taskbean);
			}
			ActiveTask at = new ActiveTask();
			at.setId(data.getId());
			at.setTaskid(data.getTaskid());
			at.setTaskcode(taskbean.getCode());
//			at.setHadget(false);
			getAtmap().put(data.getId(), at);
		}
	}
	
	public void reset(String playercode) {
		setKeynum(0);
		
		setGrowgetids(new ArrayList<Integer>());
		setGrowgetids(new ArrayList<Integer>());
		
		for(ActiveTask at : getAtmap().values()) {
			Task.remove(playercode, at.getTaskcode());
		}
		
		setAtmap(new HashMap<Integer, ActiveTask>());
		
		//宝箱
		Map<String, actweekcarkgrow> awcgmap = ActDataCenter.getAllData(actweekcarkgrow.class, playercode);
		for(actweekcarkgrow data :awcgmap.values()) {
			getGrowids().add(data.getId());
		}
		
		//活跃任务
		Map<String, actweekcarktask> awctmap = ActDataCenter.getAllData(actweekcarktask.class, playercode);
		ArrayList<Integer> groups = new ArrayList<Integer>();
 		for(actweekcarktask data : awctmap.values()) {
			if(groups.contains(data.getGroup())) {
				continue;
			}
			
			groups.add(data.getGroup());
		}
 		
 		int group = 1;
 		if(groups.size() > 0) {
 			Collections.sort(groups);
 			group = groups.get(0);
 		}
 		setTaskgroup(group);
 		
 		for(actweekcarktask data : awctmap.values()) {
			if(group != data.getGroup()) {
				continue;
			}
			
			int taskid = data.getTaskid();
			task taskdata = DataCenter.getData(taskid, task.class);
			if(taskdata == null) {
				continue;
			}
			
			Task taskbean = Task.create(playercode, taskdata);
			
			ActiveTask at = new ActiveTask();
			at.setId(data.getId());
			at.setTaskid(data.getTaskid());
			at.setTaskcode(taskbean.getCode());
//			at.setHadget(false);
			getAtmap().put(data.getId(), at);
 		}
	}
	
	public void update() {
		
	}
	
	public void check(String playercode) {
		for(int id : getGrowids()) {
			if(getGrowgetids().contains(id)) {
				continue;
			}
			
			actweekcarkgrow awcg = ActDataCenter.getData(id, actweekcarkgrow.class, playercode);
			if(getKeynum() >= awcg.getCondition()) {
				new TipHandle().updateTip(playercode, TipType.活动_周卡_活跃任务, true);
				return;
			}
		}
		
		for(int id : getAtmap().keySet()) {
			ActiveTask at = getAtmap().get(id);
			
			String taskcode = at.getTaskcode();
			Task taskbean = Task.get(playercode, taskcode);
			if(taskbean.isFinish() && !taskbean.isReward()) {
				new TipHandle().updateTip(playercode, TipType.活动_周卡_活跃任务, true);
				return;
			}
		} 
		
		new TipHandle().updateTip(playercode, TipType.活动_周卡_活跃任务, false);
	}
	
}
